import requests

# post: 表单格式参数 data
# 金融充值接口
url = "http://192.168.65.1:8088/futureloan/mvc/api/member/recharge"
cs1 = {"mobilephone": "15592215821", "amount": "1234"}
r = requests.post(url, data=cs1)
print(r.text)
assert r.json()['code'] == '10001'
print("==========================================================================================================")

# post: json格式参数 json
# 学之思登录接口
url = "http://192.168.65.1:8000/api/user/login"
cs2 = {
    "userName": "admin",
    "password": "123456",
    "remember": False
}
r = requests.post(url, json=cs2)
print(r.json())
assert r.json()['message'] == '成功'
print("==========================================================================================================")

# 上传文件
url = "http://127.0.0.1:8088/carRental/file/uploadFile.action"
path = "D:/1.jpg"
with open(path, 'rb') as f:
    # 方法1：
    cs = {"mf": (path,f)} # {'name': ('filename', fileobj)}
    # 方法2：
    cs3 = {"mf": (path, f, "image/jpeg")} # {'name': ('filename', fileobj, content_type)}

    r = requests.post(url,files=cs)
    r2 = requests.post(url, files=cs3)
    print(r.json())
    print(r2.json())
    src = r.json()['data']['src']
    src2 = r2.json()['data']['src']
    print("图片在服务器上的路径为：", src)
    print("图片在服务器上的路径为：", src2)

    print("状态码：", r.status_code)
    print("响应头：", r.headers)
    print("请求头：", r.request.headers)
    # 伪装成浏览器发送的请求
    hd = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.265.400 QQBrowser/12.7.5769.400"}
    r = requests.post(url, files=cs, headers=hd)
    print("请求头：", r.request.headers)